package com.Rolin.wss.service.impl;

import com.Rolin.wss.dao.SeatMapper;
import com.Rolin.wss.model.JspToController.SeatBO;
import com.Rolin.wss.model.SeatOrderVO;
import com.Rolin.wss.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class SeatServiceImpl implements SeatService {

    //注入DAO
    @Autowired
    SeatMapper seatMapper;

    @Override
    public ArrayList<SeatBO> getSeatInFlight(int flight) {
        ArrayList<SeatOrderVO> soo = seatMapper.selectSeatByFlight(flight);
        ArrayList<SeatBO> sbo = new ArrayList<SeatBO>();

        for (SeatOrderVO seatOrderVO : soo) {
            SeatBO seatBO  = new SeatBO(seatOrderVO.getSo_seat_no_row(),seatOrderVO.getSo_seat_no_col());
            sbo.add(seatBO);
        }
        if (sbo.size()>=1) return sbo;
        else return null;
    }

    @Override
    public SeatOrderVO getSeatInUser(int userID, int flight) {
        return seatMapper.selectSeatByUserFlight(userID,flight);
    }

    @Override
    public boolean bySeat(SeatOrderVO soo) {
        boolean res =false;
        if(seatMapper.selectSeatByUserFlight(soo.getSo_user_id(),soo.getSo_flight_id())==null){
            res = (seatMapper.insertSeat(soo.getSo_user_id(),soo.getSo_flight_id(),soo.getSo_seat_no_row(),soo.getSo_seat_no_col())==1);
        }else{
            res = seatMapper.updateSeat(soo.getSo_user_id(),soo.getSo_flight_id(),soo.getSo_seat_no_row(),soo.getSo_seat_no_col()) == 1;
        }
        return res;
    }
}
